var F_Request = {
	getRequest: function(url, initParams, successFunc = null, errorFunc = null) {
		/**
		 * get请求
		 */
		initParams = initParams ? initParams : {};
		initParams = Object.assign(initParams, {
			method: "GET"
		});
		this.RequestData(url, initParams, successFunc, errorFunc);
	},
	postRequest: function(url, initParams, successFunc = null, errorFunc = null) {

		initParams = initParams ? initParams : {};
		initParams = Object.assign(initParams, {
			method: "POST"
		});
		this.RequestData(url, initParams, successFunc, errorFunc);
	},
	putRequest: function(url, initParams, successFunc = null, errorFunc = null) {
		//put请求
		initParams = initParams ? initParams : {};
		initParams = Object.assign(initParams, {
			method: "PUT"
		});
		this.RequestData(url, initParams, successFunc, errorFunc);
	},
	uploadFileRequest: function(fileName = "upload", fileKey = "file", otherParams = {}, url, initParams,
		successFunc = null, errorFunc = null) {
		//上传文件 [支持多个文件上传]

		var formData = new FormData();
		var photos = document.querySelector("input[name='" + fileName + "'][multiple]");

		for (var param in otherParams) {
			formData.append(param, otherParams[param]);
		}

		// formData 只接受文件、Blob 或字符串，不能直接传递数组，所以必须循环嵌入
		for (let i = 0; i < photos.files.length; i++) {
			formData.append(fileKey, photos.files[i]);
		}


		initParams = initParams ? initParams : {};
		initParams.body = formData;
		initParams.method = initParams.method ? initParams.method : "POST";

		this.postRequest(url, initParams, successFunc, errorFunc);
	},
	deleteRequest: function(url, initParams, successFunc = null, errorFunc = null) {
		/**
		 * delete请求
		 */
		initParams = initParams ? initParams : {};
		initParams = Object.assign(initParams, {
			method: "DELETE"
		});
		this.RequestData(url, initParams, successFunc, errorFunc);
	},
	RequestData: function (url, initParams, successFunc = null, errorFunc = null) {

		/**
		 * 请求实现方法
		 */
		var defaultInitParams = {
			cache: 'no-cache', // *default, no-cache, reload, force-cache, only-if-cached
			credentials: 'same-origin', // include (发送带凭据的请求), same-origin, *omit 
			/* headers: {
				'user-agent': 'Mozilla/4.0 MDN Example',
				'content-type': 'application/json'
			}, */
			method: 'GET', // *GET, POST, PUT, DELETE, etc.
			//mode: 'cors', // no-cors, cors, *same-origin
			redirect: 'follow', // manual, *follow, error
			referrer: 'no-referrer', // *client, no-referrer
		};

		//如果请求方法为GET 和 HEAD 就去除body信息
		if (defaultInitParams.method == 'GET' || defaultInitParams.method == 'HEAD') {
			delete defaultInitParams.method;
		}

		if (initParams.method == 'GET' || initParams.method == 'HEAD') {
			delete initParams.method;
		}


		defaultInitParams = Object.assign(defaultInitParams, initParams);

		fetch(
				url, defaultInitParams
			)
			.then(function(response) {
				//先只处理响应状态正常的
				if (response.status === 200) {
					return response.json();
				}
				return {
					error: 300
				};
			}).then(function(response) {
				//并没有返回信息
				if (response.error == 300) {
					console.log("---请检查fetch的相关配置是否有问题---");
					return;
				}

				if (typeof(successFunc) == "function") {
					successFunc(response);
				}

			}).catch(function(error) {
				if (typeof(errorFunc) == "function") {
					errorFunc(error)
				}
			});

	}

}
